package com.hbyj.struts.action;

import java.beans.Encoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.DbTools;
import com.hbyj.util.Constants;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;


public class YjtxAction extends ActionSupport{
	private String jsonResult;
	private int total;
	private List rows = new ArrayList();
	
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}
	
	public String queryYjtx(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String timeFrom = request.getParameter("timeFrom");
		String timeTo = request.getParameter("timeTo");
		String dhNum = request.getParameter("dhNum");
		String lx = request.getParameter("lx");
		String conStr = "";
		if(!"".equals(timeFrom)){
			conStr += "and yjtx01020 >= '"+timeFrom+"'";
		}
		if(!"".equals(timeTo)){
			conStr += "and yjtx01020 <= '"+timeTo+"'";
		}
		if(!"".equals(dhNum)){
			conStr += "and yjtx01010 = '"+dhNum+"'";
		}
		if(!"".equals(lx)){
			conStr += "and yjtx01040 = '"+lx+"'";
		}else{
			conStr += "and yjtx01040 <= '2'";
		}
		if(!"".equals(conStr)){
			conStr =" where " + conStr.substring(4);
		}
		String hql = "from YjczYjtx01"+ conStr;
		String countHql = "select count(*) from YJCZ_YJTX01"+ conStr;
		total = DbTools.getDataSQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(total);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = (List)DbTools.findPageHQL(first, max,hql);
		return SUCCESS;
	}
	
	//查询湖北省及湖北省的市县
	public void queryErjiMuluForYjtx() throws UnsupportedEncodingException, IOException{
		String hql = "from YjczXzqh01 where xzqh01010 like '42__00'";
		List list = DbTools.findByHql(hql);
		JSONArray jsonArray = new JSONArray();
		if(list.size()>0){
			for(int i = 0;i<list.size();i++){
				jsonArray.add(list.get(i));
			}
		}
		ServletActionContext.getResponse().getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
	}
	
	//查询湖北省级下得单位
	public void queryHbsDw() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String yjcz09010 = request.getParameter("sxbh");
		String sql = "select distinct YJCZ09020 from YJCZ_YJCZ09 where YJCZ09010 = '"+yjcz09010+"'";
		List list = DbTools.findBySql(sql);
		JSONArray jsonArray = new JSONArray();
		if(list.size()>0){
			for(int i = 0;i<list.size();i++){
				jsonArray.add(list.get(i));
			}
		}
		ServletActionContext.getResponse().getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
	}
	
	//查询市级下得单位
	public void queryDWBySxbh() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String yjcz09010Str = request.getParameter("sxbh");
		String yjcz09010 = yjcz09010Str.substring(0,4)+"__";
		String sql = "select distinct YJCZ09020 from YJCZ_YJCZ09 where YJCZ09010 like '"+yjcz09010+"'";
		List list = DbTools.findBySql(sql);
		JSONArray jsonArray = new JSONArray();
		if(list.size()>0){
			for(int i = 0;i<list.size();i++){
				jsonArray.add(list.get(i));
			}
		}
		ServletActionContext.getResponse().getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
	}
	
	//根据单位名称查询人名和电话号码
	public void queryRyDhByDwmc() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String dwmc = request.getParameter("dwmc");
		String sql = "select YJCZ09030,YJCZ09035 from YJCZ_YJCZ09 where YJCZ09020 = '"+dwmc+"'";
		List list = DbTools.findBySql(sql);
		JSONArray jsonArray = new JSONArray();
		if(list.size()>0){
			for(int i = 0;i<list.size();i++){
				jsonArray.add(list.get(i));
			}
		}
		ServletActionContext.getResponse().getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
	}
	
	public void fasongDuanxinDiaoyong(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String fasongUrl = request.getParameter("fasongUrl");
		String temp = "";
		URL reqUrl;
		StringBuilder tempStr;
		try {
			reqUrl = new URL(fasongUrl);
			HttpURLConnection urlCon = (HttpURLConnection)reqUrl.openConnection();
			urlCon.setRequestMethod("POST");
			urlCon.setDoOutput(true);
			urlCon.getOutputStream().flush();
			urlCon.getOutputStream().close();
            InputStream in = urlCon.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(in));
            tempStr = new StringBuilder();
            while (rd.ready()&&(temp =rd.readLine())!= null) {
                tempStr.append(temp);
            }
            String resultStr = new String(tempStr);
            ServletActionContext.getResponse().getOutputStream().write(resultStr.getBytes("utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}